
***************************************************************************
*****Table 1. Distribution across Firm Types: Manufacturing Only **********
***************************************************************************

*****************preparing an all type manufacturing data*************

qui use "${hmrc_datadir}\export_ctrygroup_reg_fy", clear
tab type
tab year
tab industry
keep if industry=="manufacturing"

qui save "${hmrc_datadir}\export_all_fy_manufacturing", replace

********************distribution of firms by ownership type, including numbers, turnover, exports*

qui use "${hmrc_datadir}\export_all_fy_manufacturing", clear

*drop the duplicated firm information and keep total value of exports
*(since each firm has multiple transactions, firm_level turnover and asset would be double-counted)

bysort identifier year: egen total_svalue=sum(svalue)
keep identifier year turnover_ct600 total_assets fixed_assets type total_svalue
duplicates drop
duplicates report identifier year

*construct a table of firm distribution
gen obs=1

gen     type2="ukmnc"			if type=="mnc_uksubs"|type=="uk_mnc_parent"
replace type2="domestic group"	if type=="uk_domestic_parent"|type=="domestic_uksubs"
replace type2="foreign"			if type=="foreign_subs"
replace type2="standalone"		if type=="standalone"

collapse (sum)obs (sum)turnover (sum)total_assets (sum)fixed_assets (sum)total_svalue, by (type2)

qui save "${hmrc_resultdir}\Descriptive_stats\ownership_distribution", replace
export excel using "${hmrc_resultdir}\Descriptive_stats\ownership_distribution.xls", ///
sheetreplace firstrow(variables)


clear

***************************************************************************
***************Table 2. Summary Statistics ********************************
***************************************************************************

*Prepare the baseline estimation data set*

use  "${hmrc_datadir}\export_reg_final_ukmncs_fy_ntrans", clear

replace I_low_wedge=. if low_tax_country!=1
replace I_high_wedge=. if high_tax_country!=1

*winsorize 
foreach var in netmass svalue turnover fixed_asset{
qui sum `var',d
local p1=r(p1)
local p99=r(p99)
gen w_`var'=`var' if `var'>`p1' &`var'<`p99'
replace w_`var'=`p1' if `var'<=`p1'
replace w_`var'=`p99' if `var'>=`p99'&`var'!=.
}


*country characteristic

gen gdppc_usd=gdppc/exchange_rate
gen ln_gdppc=ln(gdppc_usd)

global ctryvar low_tax_country ln_gdppc

*firm characteristic

global firmvar lnsale profit_making related 

*key trade variable:
global tradevar w_netmass  p_weight w_svalue no_trans I_low_wedge I_high_wedge

global outcome lnp_weight

reghdfe ${outcome} r_lowwedge r_highwedge ///
, absorb(i.identifier#i.comcode#i.ctry i.year#i.identifier#i.comcode ///
i.year#i.ctry#i.comcode) residuals(lnp_resid) vce(cluster ctry_year)

keep if lnp_resid!=.

*transaction level summary statistics*

tabstat $tradevar $firmvar $ctryvar , columns(statistics) stat(mean sd p25 p50 p75 n) save
mat list r(StatTotal)
mat a=r(StatTotal)'
mat2txt, matrix(a) saving("${hmrc_resultdir}\Descriptive_Stats\translevel_estsummarystats.xls") ///
title("summary statistics of export final datas based on transaction level data") replace

clear





